/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.validation.validators;

import java.sql.Date;
import java.sql.Timestamp;

import org.apache.commons.lang3.ClassUtils;

import com.enfry.validation.IValidator;
import com.enfry.validation.config.Rule;

/**
 * 必填
 * @Title com.enfry.validation.validators.RequiredValidator
 * @author liulei
 * @date 2016年7月22日
 */
public class RequiredValidator implements IValidator {

    @Override
    public boolean execute(Object data, Class<?> type, Object value, Rule rule) {
        if (value == null) return false;
        if (ClassUtils.isAssignable(type, Object[].class))
            return ((Object[]) value).length > 0;
        else if (type == String.class)
            return ((String) value).trim().length() > 0;
        else if (type == Timestamp.class || type == Date.class) return true;
        return false;
    }

}
